package com.znsb.msfq.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.app.ZnsbApp;
import com.znsb.msfq.utils.CacheActivityUtils;
import com.znsb.msfq.utils.HttpUtils;
import com.znsb.msfq.utils.IntentUtils;
import com.znsb.msfq.utils.LogUtils;
import com.znsb.msfq.utils.ResponseUtils;
import com.znsb.msfq.utils.SPUtils;
import com.znsb.msfq.utils.ToastUtils;
import com.znsb.msfq.utils.UrlUtils;
import com.znsb.msfq.utils.ValidateUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/10
 * @modifyTime： 2017/2/10
 * @explain：
 */

public class SetPwdActivity extends BaseActivity {
    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.setpwd_img_head)
    ImageView setpwdImgHead;
    @Bind(R.id.setpwd_edit_loginpwd)
    EditText setpwdEditLoginpwd;
    @Bind(R.id.setpwd_img_delete1)
    ImageView setpwdImgDelete1;
    @Bind(R.id.setpwd_re_layout1)
    RelativeLayout setpwdReLayout1;
    @Bind(R.id.setpwd_edit_newpwd1)
    EditText setpwdEditNewpwd1;
    @Bind(R.id.setpwd_img_delete2)
    ImageView setpwdImgDelete2;
    @Bind(R.id.setpwd_re_layout2)
    RelativeLayout setpwdReLayout2;
    @Bind(R.id.setpwd_edit_newpwd2)
    EditText setpwdEditNewpwd2;
    @Bind(R.id.setpwd_img_delete3)
    ImageView setpwdImgDelete3;
    @Bind(R.id.setpwd_re_layout3)
    RelativeLayout setpwdReLayout3;
    @Bind(R.id.setpwe_tv_sure)
    TextView setpweTvSure;

    @Override
    protected int getlayoutView() {
        return R.layout.act_setpwd;
    }

    @Override
    protected void initView(View view) {
        barTvTitle.setText("设置密码");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void clickListener(View view) {

    }

    @OnClick({R.id.bar_img_back, R.id.setpwd_img_head, R.id.setpwd_img_delete1, R.id.setpwd_img_delete2, R.id.setpwd_img_delete3, R.id.setpwe_tv_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_img_back:
                finish();
                break;
            case R.id.setpwd_img_head:
                break;
            case R.id.setpwd_img_delete1:
                setpwdEditLoginpwd.setText("");
                break;
            case R.id.setpwd_img_delete2:
                setpwdEditNewpwd1.setText("");
                break;
            case R.id.setpwd_img_delete3:
                setpwdEditNewpwd2.setText("");
                break;
            case R.id.setpwe_tv_sure:
                String pwd = setpwdEditLoginpwd.getText().toString().trim();
                String newPwd = setpwdEditNewpwd1.getText().toString().trim();
                String newAgPwd = setpwdEditNewpwd2.getText().toString().trim();
                if (ValidateUtils.isUpdtePwd(pwd, newPwd, newAgPwd))
                    setUpdatePwd(pwd, newPwd, newAgPwd);
                break;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    private void setUpdatePwd(String pwd, String newPwd, String newAgPwd) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("oldPassword", pwd);
        maps.put("password", newPwd);
        maps.put("affirmPassword", newAgPwd);
        HttpUtils.getInstance(this).postAsnyRequest(UrlUtils.MSFQ_URL_UPDATEPWD, HttpUtils.getLoginMaps(maps), new ResponseUtils() {
            @Override
            public void onSucceed(String Code, JSONObject request) {
                LogUtils.i("TAG:", "updatePwd=" + request.toString());
                ToastUtils.TextToast(ZnsbApp.mContext, "重置密码成功", 2000);
                SPUtils.clear(ZnsbApp.mContext);
                CacheActivityUtils.finishOtherActivity(MainActivity.class);
                IntentUtils.startActivityAnimGeneral(SetPwdActivity.this, LoginActivity.class, null);
            }

            @Override
            public void onFailure(String result) {

            }
        });
    }

}
